package com.demon.logger

import android.annotation.SuppressLint
import android.util.Log
import timber.log.Timber
import timber.log.Timber.Tree

/**
 * <p>
 * Logger
 * </p>
 *
 * @author zhaozeyang
 * @since 2020/4/27
 */
@SuppressLint("LogNotTimber")
object Logger : ILogger {

  fun plant(tree: Tree) {
    Timber.plant(tree)
  }

  override fun d(
    tag: String,
    message: String
  ) {
    try {
      Timber.tag(tag)
      Timber.d(message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.d(tag, message)
    }
  }

  override fun d(
    tag: String,
    message: String,
    e: Throwable?
  ) {
    try {
      Timber.tag(tag)
      Timber.d(e, message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.d(tag, message, e)
    }
  }

  override fun i(
    tag: String,
    message: String
  ) {
    try {
      Timber.tag(tag)
      Timber.i(message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.i(tag, message)
    }
  }

  override fun i(
    tag: String,
    message: String,
    e: Throwable?
  ) {
    try {
      Timber.tag(tag)
      Timber.i(e, message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.i(tag, message, e)
    }
  }

  override fun w(
    tag: String,
    message: String
  ) {
    try {
      Timber.tag(tag)
      Timber.w(message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.w(tag, message)
    }
  }

  override fun w(
    tag: String,
    message: String,
    e: Throwable?
  ) {
    try {
      Timber.tag(tag)
      Timber.w(e, message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.w(tag, message, e)
    }
  }

  override fun e(
    tag: String,
    message: String
  ) {
    try {
      Timber.tag(tag)
      Timber.e(message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.e(tag, message)
    }
  }

  override fun e(
    tag: String,
    message: String,
    e: Throwable?
  ) {
    try {
      Timber.tag(tag)
      Timber.e(e, message)
    } catch (e: Exception) {
      Log.e(tag, e.message ?: "", e)
      Log.e(tag, message, e)
    }
  }
}